<template>
	<div id='system' v-loading="loading">
		<div class="content-top">
			<span class="h1">业务管理</span>
			<span class="h2">业务分配</span>
		</div>
		<div class="top-btn clearfix">
			<div class="left-content">
				<span class="tip">分配商家</span>
				<el-select v-model="value" placeholder="请选择" @change="chang1()">
					<el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value">
					</el-option>
				</el-select>
			</div>
			<div><span style="padding: 0 15px;">累计未分配需求量:{{nums}}</span>
				<el-button @click="allDist()" type="primary">分配</el-button>
			</div>
		</div>
		<el-table :data="table" style="width: 100%"  @selection-change="handleSelectionChange">
			<el-table-column
		      type="selection"
		      width="55">
		    </el-table-column>
			<el-table-column label="姓名">
		      <template slot-scope="scope">
		           <span v-if='scope.row.Member'>{{ scope.row.Member.CNName }}</span>
		           <span v-else></span>
		      </template>
		    </el-table-column>
		    <el-table-column label="性别">
		      <template slot-scope="scope">
		           <span v-if='scope.row.Member'>{{ scope.row.Member.Gender }}</span>
		           <span v-else></span>
		      </template>
		    </el-table-column>
		    <el-table-column label="手机号">
		      <template slot-scope="scope">
		           <span v-if='scope.row.Member'>{{ scope.row.Member.Tel}}</span>
		           <span v-else></span>
		      </template>
		    </el-table-column>
		    <el-table-column prop="DemandDate" label="需求日期">
		    </el-table-column>
		    <el-table-column prop="DMoney" label="需求金额">
		    </el-table-column>
		    <el-table-column label="风控报告">
		      <template slot-scope="scope">
		      		<router-link v-if='scope.row.Member' v-bind="{to:'/creditInfo/'+(scope.row.Member.ID)}"><el-button size="mini">风控报告  </el-button></router-link>
		      		<span v-else></span>
		      </template>
		    </el-table-column>
			<el-table-column label="操作">
				<template slot-scope="scope">
					<el-button v-if='scope.row.Member' size="mini" @click="admeasure(scope.row.DemandID)">分配</el-button>
					<span v-else></span>
				</template>
			</el-table-column>
		</el-table>
		<div class="pages">
			<el-pagination small :current-page="index" @current-change="handleCurrentChange" layout="prev, pager, next" :total="total" background prev-text='上一页' next-text='下一页'>
			</el-pagination>
		</div>
	</div>
</template>

<script>
	import api from '../../../api/api'
	export default {
		name: 'allot',
		data() {
			return {
				rate: 1, //分配
				input: '', //搜索
				table: [], //需求列表
				options: [], //商家列表
				value: '',
				nums:0,//需求条
				multipleSelection:[],//选择的
				multiple:[],//单个添加
				total:1,
				index:1,
				loading:true,
			}
		},
		created() {
			this.getproviderlist()
		},
		methods: {
			//翻页
			handleCurrentChange(val){
				this.loading = true
				this.index = val
		    	this.$axios.post(api.getdemand, {
					"Para": this.value,
					"PageSize": 10,
					"PageIndex": this.index - 1,
					"OrderbyString": "PName",
				}).then(res => {
					if(res.data.isOK) {
						this.loading = false
						this.total = res.data.data.Items?res.data.data.Items:1
						this.table = res.data.data.Data
					} else {
						if(res.data.msg == "Token无效" || res.data.msg == "Token缺失") {
							this.$router.push({
								path: '/admin'
							})
						} else {
							this.loading = false
							this.$message.error(res.data.msg);
						}
					}
				})
			},
			handleEdit(index, row) {
				console.log(index, row);
			},
			handleDelete(index, row) {
				console.log(index, row);
			},
			//业务分配
			chang1(){
				this.getprodemands(this.value)
				this.getdemand(this.value)
			},
			//获取商家列表
			getproviderlist() {
				this.loading = true
				this.$axios.post(api.getproviderlist, {
					"Para": "",
					"PageSize": 100,
					"PageIndex": 0,
					"OrderbyString": "PName",
				}).then(res => {
					if(res.data.isOK) {
						for(var i = 0; i < res.data.data.Data.length; i++) {
							this.options.push({
								value: res.data.data.Data[i].ID,
								label: res.data.data.Data[i].PName
							})
						}
						if(this.options.length){
							this.value = this.options[0].value
							this.getprodemands(this.value)
							this.getdemand(this.value)
							this.loading = false
						}else{
							this.loading = false
							this.$message.error('目前无商家');
							return false
						}
						
					} else {
						if(res.data.msg == "Token无效" || res.data.msg == "Token缺失") {
							this.$router.push({
								path: '/admin'
							})
						} else {
							this.loading = false
							this.$message.error(res.data.msg);
						}
					}
				})
			},
			//获取商家未分配需求总量
			getprodemands(id) {
				this.loading = true
				this.$axios.get(api.getprodemands + '/' + id).then(res => {
					if(res.data.isOK) {
						this.loading = false
						this.nums = res.data.data
					} else {
						if(res.data.msg == "Token无效" || res.data.msg == "Token缺失") {
							this.$router.push({
								path: '/admin'
							})
						} else {
							this.loading = false
							this.$message.error(res.data.msg);
						}
					}
				})
			},
			//获取可分配需求
			getdemand(id){
				this.$axios.post(api.getdemand, {
					"Para": id,
					"PageSize": 10,
					"PageIndex":this.index-1,
					"OrderbyString": "PName"
				}).then(res => {
					if(res.data.isOK) {
						this.loading = false
						this.total = res.data.data.Items?res.data.data.Items:1
						this.table = res.data.data.Data
					} else {
						if(res.data.msg == "Token无效" || res.data.msg == "Token缺失") {
							this.$router.push({
								path: '/admin'
							})
						} else {
							this.table=[]
							this.total=1
							this.loading = false
							this.$message.error(res.data.msg);
						}
					}
				})
			},
			//分配需求
			admeasure(id){
				if(this.nums == 0){
					this.$message.error('目前无需求');
					return false;
				}
				this.loading = true
				this.multiple[0] = id
				this.$axios.post(api.allotdemand,{
					"ProviderID": this.value,
					"DemandID": this.multiple
				}).then(res => {
					if(res.data.isOK) {
						this.multiple = []
						this.getprodemands(this.value)
						this.getdemand(this.value)
					} else {
						if(res.data.msg == "Token无效" || res.data.msg == "Token缺失") {
							this.$router.push({
								path: '/admin'
							})
						} else {
							this.loading = false
							this.$message.error(res.data.msg);
						}
					}
				})
			},
			//获取选择的
			handleSelectionChange(val){
				this.multipleSelection = []
				for(var i= 0;i<val.length;i++){
					this.multipleSelection.push(val[i].DemandID);
				}
			},
			//全部分配
			allDist(){
				if(this.nums <= 0){
					this.$message.error('目前无需求');
					return false;
				}
				this.loading = true
				this.$axios.post(api.allotdemand,{
					"ProviderID": this.value,
					"DemandID": this.multipleSelection
				}).then(res => {
					if(res.data.isOK) {
						this.multipleSelection = []
						this.getprodemands(this.value)
						this.getdemand(this.value)
					} else {
						if(res.data.msg == "Token无效" || res.data.msg == "Token缺失") {
							this.$router.push({
								path: '/admin'
							})
						} else {
							this.loading = false
							this.$message.error(res.data.msg);
						}
					}
				})
			}
		},

	}
</script>

<style>

</style>